package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.SystemDictionaryItem;
import com.xmg.p2p.base.domain.UserFile;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.mapper.UserFileMapper;
import com.xmg.p2p.base.query.PageResult;
import com.xmg.p2p.base.query.UserFileQueryObject;
import com.xmg.p2p.base.service.IUserFileService;
import com.xmg.p2p.base.service.IUserinfoService;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * lcd  2020/5/20
 * Description:
 */
@Service
public class UserFileServiceImpl implements IUserFileService {
    @Autowired
    private IUserinfoService userinfoService;
    @Autowired
    private UserFileMapper userFileMapper;

    @Override
    public void apply(String fileName) {
        UserFile userFile = new UserFile();
        userFile.setImage(fileName);
        userFile.setApplier(UserContext.getLogininfo());
        userFile.setApplyTime(new Date());
        userFile.setState(UserFile.STATE_NORMAL);
        userFileMapper.insert(userFile);
    }

    @Override
    public List<UserFile> query(Long logininfoId,Boolean hasFileType) {
        return userFileMapper.query(logininfoId, hasFileType);
    }

    @Override
    public void updateType(long[] fileTypes, long[] ids) {
        for (int i = 0; i < ids.length; i++) {
//            userFileMapper.updateType(ids[i],fileTypes[i]);
            UserFile userFile = userFileMapper.selectByPrimaryKey(ids[i]);
            SystemDictionaryItem systemDictionaryItem = new SystemDictionaryItem();
            systemDictionaryItem.setId(fileTypes[i]);
            userFile.setFileType(systemDictionaryItem);
            userFileMapper.updateByPrimaryKey(userFile);
        }
    }

    @Override
    public PageResult queryForPage(UserFileQueryObject qo) {
        int i = userFileMapper.queryForCount(qo);
        if (i > 0) {
            List<UserFile> userFiles = userFileMapper.queryForPage(qo);
            return new PageResult(userFiles, i, qo.getCurrentPage(), qo.getPageSize());
        }
        return PageResult.empty(qo.getPageSize());
    }

    @Override
    public void audit(long id, int state, int score, String remark) {
        //根据id找到UserFile,确定状态
        UserFile userFile = userFileMapper.selectByPrimaryKey(id);
        //设置通用属性
        if (userFile != null && userFile.getState() == UserFile.STATE_NORMAL) {
            userFile.setState(state);
            userFile.setRemark(remark);
            userFile.setAuditor(UserContext.getLogininfo());
            userFile.setAuditTime(new Date());
            if (state == UserFile.STATE_AUDIT) {
                userFile.setScore(score);
                //找到对应的前台的用户,然后设置分数
                Userinfo userinfo = userinfoService.findById(userFile.getApplier().getId());
                userinfo.setScore(userinfo.getScore()+score);
                userinfoService.update(userinfo);
            }
            userFileMapper.updateByPrimaryKey(userFile);
        }

        //审核成功,添加分数
    }
}
